package com.cgbl.cg.vm.course

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ResourceServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.ToastUtils
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class CommentInputViewModel(application: Application) : GLBaseViewModel(application) {

    private var productId = ""
    private var commentType = 0
    private var parentId = ""
    private var accountId = ""

    val imgList = ArrayList<String>()
    val contentField = ObservableField<String>()

    fun submitComment() {
        val content = contentField.get() ?: ""
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("回复内容不能为空！")
            return
        }

        when(commentType){
            AppConstants.BundleValue.COMMENT_TYPE_RESOURCE->{
                val params = mapOf(
                    "content" to content,
                    "parentId" to parentId,
                    "naturalId" to productId,
                    "toAccountId" to accountId,
                    "imageFiles" to imgList
                )
                ResourceServiceFactory.createNaturalComment(params)
                    .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                        override fun onNext(t: BaseResponse<String>) {
                            LiveDataEventManager.postCommentSuccessEvent()
                            ToastUtils.showShort("评价成功!")
                            finish()
                        }
                    })
            }
            else->{
                val params = mapOf(
                    "content" to content,
                    "parentId" to parentId,
                    "courseId" to productId,
                    "toAccountId" to accountId,
                    "imageFiles" to imgList
                )
                CourseServiceFactory.createCourseComment(params)
                    .subscribe(object : AbstractViewModelSubscriber<BaseResponse<Any>>(this) {
                        override fun onNext(t: BaseResponse<Any>) {
                            LiveDataEventManager.postCommentSuccessEvent()
                            ToastUtils.showShort("评价成功!")
                            finish()
                        }
                    })
            }
        }

    }

    fun doUpload(
        fileName: String,
        path: String,
        success: (uploadBean: String) -> Unit
    ) {
        val fileRQ: RequestBody = RequestBody.create(
            "multipart/form-data".toMediaTypeOrNull(),
            File(path)
        )
        val part = MultipartBody.Part.createFormData("file", fileName, fileRQ)
//        showLoadingDialog()
        UserServiceFactory.upload(part)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
//                    dismissLoadingDialog()
                    ToastUtils.showShort("上传成功")
                    success.invoke(t.data)
                    imgList.add(t.data)
                }
            })
    }

    fun initParams(type: Int, productId: String, parentId: String, accountId: String) {
        this.commentType = type
        this.productId = productId
        this.parentId = parentId
        this.accountId = accountId
    }
}